package dao;

import entity.User;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import utils.DataSourceUtils;

import java.util.Map;

public class UserDao {

    private JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());

    public User findByUsername(String username) {

        try {
             return jdbcTemplate.queryForObject("SELECT * FROM  `user` WHERE username = ?",new BeanPropertyRowMapper<>(User.class),username);
        } catch (DataAccessException e) {
            //只是证明没有在数据库中找到这个用户，没有必要抛异常
            return null;
        }
    }

    public User findByEmail(String email) {
        try {
            return jdbcTemplate.queryForObject("SELECT * FROM  `user` WHERE email = ?",new BeanPropertyRowMapper<>(User.class),email);
        } catch (DataAccessException e) {
            //只是证明没有在数据库中找到这个用户，没有必要抛异常
            return null;
        }
    }

    /**
     * 注册用户
     * @param param
     */
    public void register(Map<String, String> param) {
        jdbcTemplate.update(
                "INSERT INTO `user` SET email = ?, username = ?, `password` = ?",
                param.get("form-email"),
                param.get("form-username"),
                param.get("form-password")
        );
    }

    /**
     * 登录
     * @param param
     * @return
     */
    public User login(Map<String, String> param) {
        try {
            return jdbcTemplate.queryForObject("SELECT * FROM `user` WHERE username= ? AND `password`= ?; ",
                    new BeanPropertyRowMapper<>(User.class),
                    param.get("form-username"),
                    param.get("form-password"));
        } catch (DataAccessException e) {
            //只是证明没有在数据库中找到这个用户，没有必要抛异常
            return null;
        }
    }

    public User findById(Long userId) {
        try {
            return jdbcTemplate.queryForObject("SELECT * FROM `user` WHERE id= ? ", new BeanPropertyRowMapper<>(User.class), userId);
        } catch (DataAccessException e) {
            //只是证明没有在数据库中找到这个用户，没有必要抛异常
            return null;
        }
    }

    /**
     * 根据扫码登入的wxOpenId查询user
     * @param wxOpenId
     * @return
     */
    public User findByWxOpenId(String wxOpenId) {
        try {
            return jdbcTemplate.queryForObject(
                    "SELECT * FROM  `user` WHERE wx_openid = ?",
                    new BeanPropertyRowMapper<>(User.class),wxOpenId);
        } catch (DataAccessException e) {
            //只是证明没有在数据库中找到这个用户，没有必要抛异常
            return null;
        }
    }

    /**
     * 第一次微信登录，注册user
     * @param user
     */
    public void WxRegister(User user) {
        jdbcTemplate.update(
                "INSERT INTO `user` SET \n" +
                        "pic = ?, gender = ?, `real_name` = ?,\n" +
                        "username = ?, wx_openid = ?",
                user.getPic(),
                user.getGender(),
                user.getRealName(),
                user.getUsername(),
                user.getWxOpenid()
        );

    }
}
